package com.backend.demo.util;

import java.util.ArrayList; 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbTableMetadata {
	private Map<String, List<ColumnDatadata>> map;

	public void addColumn(String tableName, String columnName) {
		if (map == null) {
			map = new HashMap<String, List<ColumnDatadata>>();
		}

		List<ColumnDatadata> list = map.get(tableName.toUpperCase());
		if (list == null) {
			list = new ArrayList<DbTableMetadata.ColumnDatadata>();
			map.put(tableName.toUpperCase(), list);
		}

		list.add(new ColumnDatadata(tableName.toUpperCase(), columnName
				.toUpperCase()));
	}

	public boolean containsTable(String tableName) {
		if (map == null || tableName == null) {
			return false;
		}

		List<ColumnDatadata> columns = map.get(tableName.toUpperCase());
		if (CollectionUtils.empty(columns)) {
			return false;
		}

		return true;
	}

	public boolean containsColumn(String tableName, String columnName) {
		if (map == null) {
			return false;
		}

		List<ColumnDatadata> columns = map.get(tableName.toUpperCase());
		if (CollectionUtils.empty(columns)) {
			return false;
		}

		for (ColumnDatadata da : columns) {
			if (da.columnName.equalsIgnoreCase(columnName.toUpperCase())
					&& da.tableName.equalsIgnoreCase(tableName.toUpperCase())) {
				return true;
			}
		}
		return false;
	}

	public static class ColumnDatadata {
		private String tableName; // 表名
		private String columnName; // 字段名
		private String tableComment; // 表备注
		private String columnComment; // 字段备注

		public ColumnDatadata(String tableName, String columnName) {
			super();
			this.tableName = tableName;
			this.columnName = columnName;
		}

		public ColumnDatadata(String tableName, String columnName,
				String tableComment, String columnComment) {
			super();
			this.tableName = tableName;
			this.columnName = columnName;
			this.tableComment = tableComment;
			this.columnComment = columnComment;
		}

		public String getTableName() {
			return tableName;
		}

		public void setTableName(String tableName) {
			this.tableName = tableName;
		}

		public String getColumnName() {
			return columnName;
		}

		public void setColumnName(String columnName) {
			this.columnName = columnName;
		}

		public String getTableComment() {
			return tableComment;
		}

		public void setTableComment(String tableComment) {
			this.tableComment = tableComment;
		}

		public String getColumnComment() {
			return columnComment;
		}

		public void setColumnComment(String columnComment) {
			this.columnComment = columnComment;
		}
	}
}
